package cn.itcast.myapplication.ui.dashboard;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import cn.itcast.myapplication.AddCourseActivity;
import cn.itcast.myapplication.DatabaseOpenHelper;
import cn.itcast.myapplication.adapter.CourseAdapter;
import cn.itcast.myapplication.bean.Course;
import cn.itcast.myapplication.databinding.FragmentDashboardBinding;

public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();


        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onResume() {
        super.onResume();
        DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(getActivity());
        List<Course> courseList = databaseOpenHelper.getAllCourses();
        CourseAdapter courseAdapter = new CourseAdapter(courseList, getActivity());
        binding.recyclerView.setAdapter(courseAdapter);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        binding.imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到添加课程界面
                Intent intent = new Intent(getActivity(), AddCourseActivity.class);
                startActivity(intent);
            }
        });

        boolean isAdmin = getActivity().getSharedPreferences("data", MODE_PRIVATE).getBoolean("isAdmin", false);
        if (isAdmin) {
            binding.imageButton.setVisibility(View.VISIBLE);
        } else {
            binding.imageButton.setVisibility(View.GONE);
        }





    }
}